package ai4games_tests.soccer;

import game2dai.World;
import game2dai.entities.BaseEntity;
import game2dai.fsm.State;
import game2dai.fsm.Telegram;

public class TeamGlobal extends State implements Constants{

	private static TeamGlobal instance = null;

	public static TeamGlobal state(){
		if(instance == null)
			instance = new TeamGlobal();
		return instance;
	}

	@Override
	public void enter(BaseEntity owner) {
	}

	@Override
	public void execute(BaseEntity owner, double deltaTime, World world) {
		Team team = (Team) owner;
		if(team.pitch.ballInPlay){
			team.calculateClosestPlayerToBall();
		}
	}

	@Override
	public void exit(BaseEntity owner) {
	}

	@Override
	public boolean onMessage(BaseEntity owner, Telegram tgram) {
		Team team = (Team) owner;
		switch(tgram.msg){
		case AT_TARGET:
			if(team.nbrReachedTarget == team.players.length)
				team.nbrReachedTarget = 1;
			else
				team.nbrReachedTarget++;
			break;
		}
		return false;
	}

}
